<?php

/**
 * @Author: liu
 * @Date:   2018-07-11 10:27:34
 * @Last Modified by:   liu
 * @Last Modified time: 2018-07-12 09:37:57
 * 用于
 */
class PMSPurchaseController extends BaseController {
    /**
     *  列表
     * */
    public function purchaseList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
                "type"=>'integer', //1行政采购 2市场采购 3门店采购 4工程采购 5其他
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $page_size = Input::get("page_size",25);
            $page = Input::get("page",1);

            $sls = DB::table("pms_dept_purchase")->where("status",1)->select("id","type","dept_name","purchase_date","person_name","item_name","item_type","item_type_desc","brand_name","specifications","number","price","small_plan","suppller_name","suppller_type","shop_name","contacts","phone","url","desc")->orderBy('id','desc');
            if(Input::get("type")){
                $sls->where("type","like",Input::get("type"));
            }
            if(Input::get("dept_name") != ""){
                $sls->where("dept_name","like",'%'.Input::get("dept_name").'%');
            }
            
            $count = $sls->count();
            $sls = $sls->skip($page_size*($page-1))->take($page_size)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ["count"=>$count,"lists"=>$sls],
                'extra'=>['type'=>array(1=>"行政采购", 2=>"市场采购", 3=>"门店采购", 4=>"工程采购", 5=>"其他"),'suppller_type'=>array(1=>"普通", 2=>"淘宝", 3=>"天猫", 4=>"京东", 5=>"其他电商"),"item_type"=>array(1=>"设备工具", 2=>"办公设备", 3=>"展板标识", 4=>"宿舍设施",  5=>"后勤日用", 6=>"装修施工", 7=>"交通工具", 8=>"其他")]);
        }while(false);
        // Log::info("info", array("system" => "pms" ,"event" => "list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    

    /**
     * 保存
     */
    public function create(){
        $rules = array(
            'token' => 'required',
            "type"=>'required|integer',//
            "dept_name"=>'required',
            "purchase_date"=>'required',
            "person_name"=>'required',
            "item_name"=>'required',
            "item_type"=>'required|integer',
            "number"=>'required',
            "price"=>'required',
            "suppller_name"=>'required',
            "suppller_type"=>'required',
            "contacts"=>'required',
            "phone"=>'required',
            "url"=>'required',
        );
        
        $validator = Validator::make(Input::all(),$rules);
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                $response["result"] = $validator->messages()->all();                
                break;
            }            
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            if(!Input::get("id")){
                DB::table('pms_dept_purchase')->insert(array(
                    'type' => Input::get("type"),//采购类型
                    'dept_name' => Input::get("dept_name"),//采购需求部门
                    'purchase_date' => Input::get("purchase_date"),//计划采购时间
                    'person_name' => Input::get("person_name"),//采购承接人
                    'item_name' => Input::get("item_name"),//物品名称
                    'item_type' => Input::get("item_type"),//物品分类
                    'item_type_desc' => Input::get("item_type_desc",""),//物品分类说明
                    'brand_name' => Input::get("brand_name",""),//品牌
                    'specifications' => Input::get("specifications",""),//规格型号
                    'number' => Input::get("number"),//数量
                    'price' => Input::get("price"),//单价 元
                    'small_plan' =>round(Input::get("price")*Input::get("number"),2),//小计
                    'suppller_name' => Input::get("suppller_name"),//供应商名字
                    'suppller_type' => Input::get("suppller_type"),//供应商类型
                    'shop_name' => Input::get("shop_name",""),//商铺名字
                    'contacts' => Input::get("contacts"),//联系人
                    'phone' => Input::get("phone"),//电话
                    'url' => Input::get("url"),//网址
                    'desc' => Input::get("desc",""),//备注
                    'status'=>1,
                    "created_by"=>$res['user']->id,
                    "created_at"=>Common::currentTimeString(),
                ));
            }else{
                DB::table('pms_dept_purchase')->where("id",Input::get("id"))->update(array(
                    'type' => Input::get("type"),//采购类型
                    'dept_name' => Input::get("dept_name"),//采购需求部门
                    'purchase_date' => Input::get("purchase_date"),//计划采购时间
                    'person_name' => Input::get("person_name"),//采购承接人
                    'item_name' => Input::get("item_name"),//物品名称
                    'item_type' => Input::get("item_type"),//物品分类
                    'item_type_desc' => Input::get("item_type_desc",""),//物品分类说明
                    'brand_name' => Input::get("brand_name",""),//品牌
                    'specifications' => Input::get("specifications",""),//规格型号
                    'number' => Input::get("number"),//数量
                    'price' => Input::get("price"),//单价 元
                    'small_plan' =>round(Input::get("price")*Input::get("number"),2),//小计
                    'suppller_name' => Input::get("suppller_name"),//供应商名字
                    'suppller_type' => Input::get("suppller_type"),//供应商类型
                    'shop_name' => Input::get("shop_name",""),//商铺名字
                    'contacts' => Input::get("contacts"),//联系人
                    'phone' => Input::get("phone"),//电话
                    'url' => Input::get("url"),//网址
                    'desc' => Input::get("desc",""),//备注
                    "updated_by"=>$res['user']->id,
                    "updated_at"=>Common::currentTimeString(),
                ));
            }
            

            
        }while(false);
        Log::info("info", array("system" => "pms_dept" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    public function delete(){
        $rules = array(
            'token' => 'required',
            "id"=>'required',//
        );
        
        $validator = Validator::make(Input::all(),$rules);
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                $response["result"] = $validator->messages()->all();                
                break;
            }            
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            DB::table('pms_dept_purchase')->where("id",Input::get("id"))->update(array("status"=>0,"updated_by"=>$res['user']->id,"updated_at"=>Common::currentTimeString()));
            
        }while(false);
        Log::info("info", array("system" => "pms_dept" ,"event" => "delete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-07-12T09:33:36+0800
     * @return   [type]                   [description]
     */
    public function purchaseShow(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "id"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }

            $sls = DB::table("pms_dept_purchase")->where("id",Input::get("id"))->select("id","type","dept_name","purchase_date","person_name","item_name","item_type","item_type_desc","brand_name","specifications","number","price","small_plan","suppller_name","suppller_type","shop_name","contacts","phone","url","desc")->first();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("info"=>$sls));
        }while(false);
        // Log::info("info", array("system" => "pms" ,"event" => "list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}